package com.maximus.server.nio.multithreadingReactor;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class AcceptorMulti implements Runnable {
    private static final Logger logger = LogManager.getLogger(AcceptorMulti.class);

    private ServerSocketChannel serverSocketChannel;

    private Selector selector;

    public AcceptorMulti(ServerSocketChannel serverSocketChannel, Selector selector) {
        this.serverSocketChannel = serverSocketChannel;
        this.selector = selector;
    }

    @Override
    public void run() {
        SocketChannel socketChannel = null;
        try {
            socketChannel = serverSocketChannel.accept();
            socketChannel.configureBlocking(false);
            SelectionKey selectionKey = socketChannel.register(selector, SelectionKey.OP_READ);
            selectionKey.attach(new HandlerMulti(socketChannel));
        } catch (IOException e) {
            logger.error(e);
            throw new RuntimeException(e);
        }
    }
}
